
import java.util.Scanner;

public class Main3 {
    private static Scanner in;
    public static void main(String[] args)
    {
        in=new Scanner(System.in);
        int n;
        n=in.nextInt();
        String[] id=new String[n];
        input(n,id);

      while(true)
        {
            String s=in.next();

            switch(s){
                case "sort1":
                    Sort(id,n);
                    for(int i=0;i<n;i++)
                    {
                        System.out.println(id[i].substring(6,10)+"-"+id[i].substring(10,12)+"-"+id[i].substring(12,14));
                    }
                    break;
                case "sort2":
                    Sort(id,n);
                    for(String e:id)
                    {
                        System.out.println(e);
                    }
                    break;
                default:
                    System.out.println("exit");
                    System.exit(0);
                    break;
            }
        }

    }
    public static void input(int n,String[] id)
    {
        for(int i=0;i<n;i++)
        {
            id[i]=in.next();
        }
    }
    public static void Sort(String[] s,int n)
    {
        int k=0;
        for (int i=0;i<n-1;i++)
            for (int j=0;j<n-i-1;j++)
            {
                k=(s[j].substring(6,14)).compareTo(s[j+1].substring(6,14));
                if(k>0)
                {
                    String temp;
                    temp=s[j];
                    s[j]=s[j+1];
                    s[j+1]=temp;
                }
            }
    }
}